<template>
    <Layout>
        <Form ref="queryForm" :model="dataToQuery" :label-width="125">
            <Row  class="query-form">
                <Col span="8">
                    <FormItem label="所属单位：">
                        <cascaderDom ref="queryOrgCascader" @setOrgInfo="setOrgId" :rootOrgId="companyId" class="query-form-el" :orgTree="true"></cascaderDom>
                    </FormItem>
                </Col>
                <Col span="8">
                    <FormItem label="活动主题：">
                        <Select v-model="dataToQuery.topicActivityId" filterable class="query-form-el">
                            <Option v-for="item in topics" :value="item.topicActivityId" :key="item.topicActivityId">{{ item.themeName }}</Option>
                        </Select>
                    </FormItem>
                </Col>
                <!--        <Col span="8">
                          <FormItem label="创建时间：" prop="content" >
                            <DatePicker class="query-form-el" v-model="dataToQuery.createDateRange" format="yyyy-MM-dd" type="daterange" placement="bottom-end" placeholder="请选择创建时间段"
                                        @on-change="(date) => onSelectFieldQueryDateRange(date, 'createDate')"	></DatePicker>
                          </FormItem>
                        </Col>-->
                <Col span="8" v-if="!switchFlag">
                    <Button @click="switchQuery(true)" class="ivu-btn-switch" type="text">展开∨</Button>
                    <Button @click="reset1" class="ivu-btn-reset">重置</Button>
                    <Button type="primary" class="ivu-btn-query" icon="md-search" @click="query">查询</Button>
                </Col>
                <Col span="8" v-else>
                    <FormItem label="是否填报：">
                        <Select v-model='dataToQuery.isTrue' clearable class='query-form-el'>
                            <Option value="1">已填报</Option>
                            <Option value="0">未填报</Option>
                        </Select>
                    </FormItem>
                </Col>
            </Row>
            <Row v-show="switchFlag" class="query-form">
                <Col span="8">
                    <FormItem label="企业搜索：">
                        <Input class="query-form-el" placeholder="输入公司名称" v-model="dataToQuery.orgName"></Input>
                    </FormItem>
                </Col>
               <!-- <Col  span="8" >
                    <FormItem label='正副处：'>
                        <Select v-model='dataToQuery.adminLevel' clearable class='query-form-el'>
                            <Option value="AD-000001000000001">正处</Option>
                            <Option value="AD-000001000000002">副处</Option>
                            <Option value="AD-000001000000003">副处以下</Option>
                        </Select>
                    </FormItem>
                </Col> -->
                <Col span="8" offset=8>
                    <Button @click="switchQuery(false)" class="ivu-btn-switch" type="text">收起∧</Button>
                    <Button @click="reset1" class="ivu-btn-reset">重置</Button>
                    <Button type="primary" class="ivu-btn-query" icon="md-search" @click="query">查询</Button>
                </Col>
            </Row>
            <Row class="query-form">
                <Col span="24">
                    <!--<Button size="large" class="ivu-btn-add" icon="md-add" @click="addInfo">新增</Button>-->
                    <Button type="primary" class="ivu-btn-add"   icon="md-cloud-download"  @click="export1">导出</Button>
                    <Button type="primary" class="ivu-btn-add"   icon="md-cloud-download"  @click="showOrgFilter">组织机构设置</Button>
                </Col>
            </Row>
        </Form>
        <Table ref='table' :height="tableHeight" no-data-text="没有查询到相关数据!" @on-selection-change='getSelectRows'
               :loading='pageStatus.loading'
               border :columns="columns1" :data="dataTable">
        </Table>
        <Row style="margin-right:10px;">
            <Col span="24">
                <Page style="float: right" :total="pagedata.total" :page-size="pagedata.pageSize"
                      @on-change="pageChange" @on-page-size-change="pageSizeChange" :page-size-opts="pageSizeArray"
                      show-total show-sizer show-elevator/>
            </Col>
        </Row>

        <Modal v-model="orgFilter" :transfer=false :width=900  :height=600 :closable="false" :mask-closable="false"
               title="组织机构设置">
            <Table row-key='orgId' height="500" :loading="buttonLoading" class="table" no-data-text="没有查询到相关数据!"
                   highlight-row stripe border :columns="orgColumn" :load-data="handleLoadData" width="100%" :data="orgDatas" size="small">
            </Table>
            <div slot="footer">
                <Button type="text" class='ivu-btn-cancel' @click="orgFilter = false">关闭</Button>
            </div>
        </Modal>
    </Layout>
</template>

<script>
    import {Api} from '@/api/index.js'
    var cascaderDom = () => import('@/components/cascader-dom.vue');
    import {sessionStorage, cookie} from '@/common/cacheUtils.js';
    import pageCommonFunction from '@/components/pageCommonFunction.js'
    export default {
        components: {cascaderDom},
        data() {
            return {
                ...this.createCommonDataField(),
                orgFilter:false,
                orgDatas:[],
                orgList:[],
                queryMeetingSelectModal:false,
                columns1: [
                    {title: '序号', width: 80, fixed: 'left', align: 'center', render: this.indexColumnsRender},
                    {key: 'themeName', title: '活动主题', align: 'center', minWidth: 200},
                    {key: 'staffName', title: '创建人', align: 'center', minWidth: 200},
                    {key: 'twoLevelName', title: '二级集团', fixed: 'left', align: 'center', minWidth: 200},
                    {key: 'companyName', title: '单位', align: 'center', minWidth: 200},
                    // {key: 'levelName', title: '正副处', align: 'center', minWidth: 100},
                    {key: 'number', title: '主题填报情况', align: 'center', minWidth: 100,
                        render: (h, params) => {
                            return h('span', params.row.number > 0 ? '已填报' : '未填报')
                        }
                    },
                ],
                tableHeight: 10,
                companyId:'',
                buttonLoading:false,
                // api
                queryUrl: Api.topicWork+'/statistic',
                exportUrl: Api.topicWork+'/statistic/export',
                topics: [],
                topicActivityId: '',
                orgColumn:[
                    {
                        title: '组织机构名称',
                        minWidth: 200,
                        align: 'left',
                        key: 'orgName',
                        tree:true
                    },
                    {
                        title: '操作',
                        align: 'center',
                        width: 220,
                        render:(h, params) => {
                            return h("RadioGroup",
                                {
                                    props: {
                                        value: params.row.selected == '1' ? '不统计' : '统计'
                                    },
                                    on: {
                                        "on-change": (val) => {
                                            val == '不统计' ? params.row.selected = '1' : params.row.selected = '0'
                                            let url = Api.insertTopicOrg;
                                            var data = {
                                                orgId : params.row.orgId,
//                                                twoLevelOrg:  sessionStorage('userId').getObject('userId').twoLevelOrgId,
                                                twoLevelOrg:  sessionStorage('userId').getObject('userId').companyId,
                                                selected: params.row.selected
                                            }
                                            this.$http.post(url,data).then((res) => {[]
                                            }).catch(e => {
                                                this.$Message.error("修改异常")
                                            })
                                        }
                                    }
                                },
                                [
                                    h("Radio", {
                                        style: {
                                            display: "inline-block",
                                            "font-size": "14px"
                                        },
                                        props: {
                                            label: "统计",
                                        }
                                    }),
                                    h("Radio", {
                                        style: {
                                            display: "inline-block",
                                            "font-size": "14px"
                                        },
                                        props: {
                                            label: "不统计",
                                        }
                                    })
                                ]
                            );
                        }
                    }
                ],
            }
        },
        mounted() {
            this.companyId=sessionStorage('userId').getObject('userId').companyId
            let data = {
                orgId: this.companyId
            };
            this.tableHeight = window.innerHeight - this.$refs.table.$el.offsetTop - 145;
            this.$http.get(Api.topicActivityList,{
                params: data
            }).then((res) => {
                if(res.data.status == 200){
                    this.topics = res.data.data.data;
                    if (this.topics.length > 0) {
                        this.topicActivityId = this.topics[0].topicActivityId;
                        this.dataToQuery.topicActivityId = this.topics[0].topicActivityId
                        this.query();
                    }
                }
            }).catch(e => {
                this.$Message.error("附件删除失败");
            });
        },
        methods: {
            ...pageCommonFunction,
            export1(){
                if (this.dataToQuery.adminLevel == null || this.dataToQuery.adminLevel == undefined ) {
                    delete this.dataToQuery['adminLevel']
                }
                if (this.dataToQuery.isTrue == null || this.dataToQuery.isTrue == undefined) {
                    delete this.dataToQuery['isTrue']
                }
                if (this.dataToQuery.companyId != null&&this.dataToQuery.companyId != undefined&&this.dataToQuery.companyId != '') {
                    this.dataToQuery.orgId=this.dataToQuery.companyId
                }else {
                    this.dataToQuery.orgId = sessionStorage('userId').getObject('userId').companyId
                }
                this.dataToQuery.twoLevelOrg = sessionStorage('userId').getObject('userId').companyId
                this.dataToQuery.type = 'topicWork'
                this.exportFormSubmit()
            },
            showOrgFilter(){
                this.getOrgInfo();
                this.orgFilter = true;
            },
            saveOrgFilter(){
                this.orgList.forEach(item=>{
                    console.log(item.selected + "  " + item.orgId);
                })
            },
            handleLoadData (item, callback) {
                setTimeout(() => {
                    item.children = [];
                    this.orgList.forEach(i => {
                        if (i.parentId == item.orgId) {
                            var temp = {};
                            temp._loading = false;
                            temp.children = [];
                            temp.selected = i.selected;
                            temp.orgName = i.orgName;
                            temp.orgId = i.orgId;
                            item.children.push(temp);
                        }
                    })
                    callback(item.children);
                }, 2000);
            },
            getOrgInfo(){
                this.buttonLoading = true;
//                var twoLevelOrg = sessionStorage('userId').getObject('userId').twoLevelOrgId == null? sessionStorage('userId').getObject('userId').companyId;
                this.$http.get(Api.queryOrgFromTopic + "/" + sessionStorage('userId').getObject('userId').companyId).then(res => {
                    this.orgDatas = [];
                    this.orgList = res.data.data;
                    this.orgList.forEach(item=>{
//                        if(item.parentId == sessionStorage('userId').getObject('userId').twoLevelOrgId){
                        if(item.parentId == sessionStorage('userId').getObject('userId').companyId){
                            var temp = {};
                            temp._loading = false;
                            temp.children = [];
                            temp.selected = item.selected;
                            temp.orgName = item.orgName;
                            temp.orgId = item.orgId;
                            this.orgDatas.push(temp);
                        }
                    })
                    this.buttonLoading = false;
                }).catch(e => {
                    this.buttonLoading = false;
                    this.$Message.error('请求失败');
                })
            },
            updateChildren(item,orgInfo){
                var i = 0;
                item.forEach(item1=>{
                    if(item1.orgId == orgInfo.orgId){
                        this.getOrgInfoTree(orgInfo.orgId,item1);
                        return;
                    }else{
                        if(item1.children != undefined)
                            this.updateChildren(item1.children,orgInfo);
                    }
                    i++;
                })
            },
            getOrgInfoTree(orgId,currentOrgInfo){
                this.$http.get(Api.queryOrg, {
                    params: {
                        parentId: orgId,
                        codeAffordIf : 0
                    }
                }).then(res => {
                    res.data.data.map(i => {
                        if(i.orgLevel != '0200100005000000007'){
                            i._loading = false;
                            i.children = [];
                        }
                    })
                    if(orgId == "-1"){
                        this.orgDatas = [];
                        this.orgDatas.push(res.data.data[0]);
                    }else{
                        this.$set(currentOrgInfo,'children',res.data.data);
                        this.$set(currentOrgInfo,'_showChildren',true);
                        delete currentOrgInfo['_loading'];
                    }
                }).catch(e => {
                    this.$Message.error('请求失败');
                })
            },
            query() {
                if (this.dataToQuery.adminLevel == null || this.dataToQuery.adminLevel == undefined ) {
                    delete this.dataToQuery['adminLevel']
                    // this.dataToQuery.adminLevel=null
                }
                if (this.dataToQuery.isTrue == null || this.dataToQuery.isTrue == undefined) {
                    delete this.dataToQuery['isTrue']
                }
                if (this.dataToQuery.companyId != null&&this.dataToQuery.companyId != undefined&&this.dataToQuery.companyId != '') {
                    this.dataToQuery.orgId=this.dataToQuery.companyId
                }else {
                    this.dataToQuery.orgId = sessionStorage('userId').getObject('userId').companyId
                }
//                this.dataToQuery.twoLevelOrg = sessionStorage('userId').getObject('userId').twoLevelOrgId;
                this.dataToQuery.twoLevelOrg = sessionStorage('userId').getObject('userId').companyId
                this.dataToQuery.type = 'topicWork'
                let data = {
                    startPage: this.pagedata.currentPage,
                    size: this.pagedata.pageSize,
                    conditions: this.dataToQuery
                };
                this.$http.post(this.queryUrl,data).then((res) => {
                    this.dataTable = res.data.data.data;
                    this.pagedata.total = res.data.data.totalSize;
                    var staffName = "";
                    var topicName = "";
                    for(var i=0;i<this.topics.length;i++){
                        if(this.dataToQuery.topicActivityId == this.topics[i].topicActivityId){
                            topicName = this.topics[i].themeName;
                            staffName = this.topics[i].staffName;
                        }
                    }
                    this.dataTable.forEach(item=>{
                        item.themeName = topicName;
                        item.staffName = staffName;
                    })
                }).catch(e => {
                    this.$Message.error("数据获取失败")
                })
            },
            reset1() {
                this.dataToQuery = {
                    topicActivityId : this.topicActivityId
                };
                if (this.$refs.queryOrgCascader) {
                    this.$refs.queryOrgCascader.resetCurrentOrg();
                }
                if (this.$refs.queryOrgCascader1) {
                    this.$refs.queryOrgCascader1.resetCurrentOrg();
                }
                this.pagedata.currentPage=1
                this.query()
            },
            setOrgId(data) {
                this.dataToQuery.companyId = data[data.length - 1];
            },
            querySelect() {
                this.queryMeetingSelectModal = true
            }
        },
        computed: {},

    }
</script>

<style lang='css' scoped>
    @import '../../../index.css';
</style>
